package dao.impl;

import dao.BaseDao;
import dao.intf.UserDao;
import model.User;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiaohongtao
 * @version 1.0
 * @since 2020年09月01日
 */
@Repository
public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public List<User> list() {
        String sql = "select * from user";
        List<User> list = new ArrayList<>();

        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            cn = getCn8();
            st = cn.createStatement();
            rs = st.executeQuery(sql);

            while (rs.next()) {
                User user = new User(rs.getLong(1), rs.getString(2), rs.getString(2), rs.getString(3));
                list.add(user);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeAll(cn, st, rs);
        }
        return list;
    }
}
